//
//  LoginPasswordViewController.swift
//  DiDiRecycle
//
//  Created by abb on 16/7/29.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD
import IQKeyboardManagerSwift
class LoginPasswordViewController: AbbBaseViewController {
    
    /// 电话号码
    var phoneNumber: String?
    /// 密码
    var password: String?
    
    override var userTransparentNavigationBar: Bool {
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        if let iconUrl = User.loadUser()?.userImageUrlHd{
            userImageView.sd_setImageWithURL(iconUrl)
        }
        if let phoneNum = User.loadUser()?.phoneNumber {
            userView.textField.text = phoneNum
        }
    }
    /**
     登录
     */
    func LoginClick() {
        // 键盘消失
        IQKeyboardManager.sharedManager().resignFirstResponder()
        // 等待
        SVProgressHUD.show()
        // 请求
        UserDAO.getUserByPassword(phoneNumber!, password: password!) { (bool, result,error) in
            if bool {
                    let user = User(dict: result!)
                        User.updataUser(user)
                        SVProgressHUD.dismiss()
                        NSNotificationCenter.defaultCenter().postNotificationName(ABBRootViewControllerKey, object: true)
//                }

            } else {
                SVProgressHUD.showErrorWithStatus(error)
            }
        }
        
    }
    
    private func setupUI() {
        // 添加子控件
        view.addSubview(userImageView)
        view.addSubview(userView)
        view.addSubview(passwordView)
        view.addSubview(loginBtn)
        view.addSubview(userAgreementBtn)
        // 布局子控件
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).multipliedBy(0.5)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        userView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_centerY).multipliedBy(0.8)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.height.equalTo(42)
        }
        passwordView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(userView.snp_bottom).offset(20)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.height.equalTo(42)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(passwordView.snp_bottom).offset(80)
            make.width.equalTo(self.view).multipliedBy(0.85)
            make.height.equalTo(42)
        }
        userAgreementBtn.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(loginBtn.snp_bottom).offset(40)
        }
        // 设置监听
        loginBtn.addTarget(self, action: #selector(LoginClick), forControlEvents: .TouchUpInside)
        // 背景
        view.backgroundColor = UIColor.whiteColor()
    }
    // MARK: - 懒加载
    /// 头像
    private lazy var userImageView:UIImageView = {
        let iv = UIImageView(image: UIImage(named:"user_default"))
        // 圆形
        iv.layer.cornerRadius = 50
        //剪边
        iv.clipsToBounds = true
        return iv
    }()
    /// 账号输入框
    private lazy var userView: FormsView = {
        let fv = FormsView { (text) in
            // 赋值
            self.phoneNumber = text
            // 判断输入的号码是否已归档
            if text == User.loadUser(self.phoneNumber!,onlyRead: "1")?.phoneNumber {
                // 判断是否有头像
                if let iconUrl = User.loadUser()?.userImageUrl{
                    // 设置头像
                    self.userImageView.sd_setImageWithURL(iconUrl)
                }
            }
        }
        fv.btn.setTitle("+86", forState: .Normal)
        fv.btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        fv.btn.titleLabel?.textAlignment = NSTextAlignment.Left
        fv.btn.titleLabel?.font = UIFont.systemFontOfSize(14)
       return fv
    }()
    /// 密码输入框
    private lazy var passwordView: FormsView = {
        let fv = FormsView(CallBack: { (text) in
            self.password = text
        })
        fv.secureTextEntry = true
        fv.btn.setImage(UIImage(named:"password"), forState: .Normal)
        return fv
    }()
    /// 登录按钮
    private lazy var loginBtn: UIButton = UIButton.buttonWithColor("登录")
    /// 用户协议
    private lazy var userAgreementBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"check"), forState: .Normal)
        btn.setTitle("用户协议", forState: .Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(12)
        return btn
    }()
}